<?php 


class Application_Default_Plugins_Acl extends Zend_Controller_Plugin_Abstract {
  private $_acl = null;
 
  public function __construct() {
      	
  	#$aclXml = new Zend_Config_Xml(APPLICATION_PATH . '/default/config/acl.xml');
	#$acl = new Application_Acl();
	#$acl->loadXml($aclXml) ;
  	$acl=new Valuerisk_Acl_Acl();
  	#$this->_acl = $acl;
    //$this->storage = new Zend_Auth_Storage_Session();
    
  	$this->_acl = $acl;
  	
  }
 
  public function preDispatch(Zend_Controller_Request_Abstract $request) {
    //As in the earlier example, authed users will have the role user
   
  	/*$session = Zend_Registry::get('originalRequest');
			$session->module = $request->module;;
			$session->controller = $request->controller;
			$session->action = $request->action;*/
  	
  	$identity = Zend_Auth::getInstance()->getIdentity();
    
  	if(isset($identity))
    	$role = $identity->role;
    else  
      	$role = 'visitor'; 
 
    $module = $request->module;      
    $controller = $request->controller;
    $action = $request->action;

    
    //For this example, we will use the controller as the resource:
  	$auth = false;
    
    if ($this->_acl->has($module)) {
		$auth = $this->_acl->isAllowed($role, $module, 'view');
		
	}
  	if ($this->_acl->has($module .'/'.$controller)) {
		$auth = $this->_acl->isAllowed($role, $module.'/'.$controller, 'view');
		
	}
    
	if($this->_acl->has($module .'/'.$controller .'/'.$action)) {
		$auth = $this->_acl->isAllowed($role, $module.'/'.$controller.'/'.$action, 'view');
	} 
	
	
     
    if(!$auth) {
      //If the user has no access we send him elsewhere by changing the request
      $request->setModuleName('default')->setControllerName('user')->setActionName('login');
    }
    
  }
}